<?php
namespace Admin\Controller;
use Think\Controller;
class LinkController extends BaseController {
	
    public function link(){
        
		$link=M('link')			
					->order('link_addtime DESC')			
					->select();

		if(!empty($_GET['value'])){
		$id=$_GET['id'];
		$value=$_GET['value'];
		$data=array(
			'link_name'=>$value,
		);
		
		$res=M('link')->where("link_id=$id")->save($data);
		if($res){
			echo 1;die;
		}else{
			echo 2;die;
		}
	}
		$a=1;
		$this->assign('link',$link);
		$this->assign('a',$a);
		$this->display();
    }
	
	public function link_add(){
		
		if(I('post.')){
			$data=array(
				'link_name'=>I('post.link_name'),
				'link_url'=>I('post.link_url'),		
				'link_addtime'=>strtotime(I('post.link_addtime')),
				'link_ord'=>I('post.link_ord'),			
				
			);
			$upload=new \Think\Upload();
			$upload->exts=array('jpg','png','gif');
			$upload->rootPath='./Upload/';
			$upload->savePath='Link/';
			$info=$upload->upload();
			
			if($info){
				$data['link_img']=$info['link_img']['savepath'].$info['link_img']['savename'];
			}
			$res=M('link')->add($data);
			
			if($res){
				$this->success('添加成功',U('Admin/Link/link'));
				exit();
			}else{
				$this->error('数据输入有误');
				exit();
			}
		}
		$this->display();
	}
	//编辑文章
	public function link_edit(){
		
		//接收参数
		$aid=I('get.aid');
		$link=M('link')->where("link_id=$aid")->find();
		$this->assign('link',$link);
		if(I('post.')){
			$data=array(
				'link_name'=>I('post.link_name'),
				'link_url'=>I('post.link_url'),		
				'link_addtime'=>strtotime(I('post.link_addtime')),
				'link_ord'=>I('post.link_ord'),			
				
			);
			//接收隐藏域的id
			$link_id=I('post.link_id');
			if($_FILES['link_img']['size']){
				$oldarr=M('link')->field('link_img')->where("link_id='$link_id'")->find();
				$path='./Upload/'.$oldarr['link_img'];
				if(!empty($oldarr) && file_exists($path)){
					unlink($path);
				}
				$upload=new \Think\Upload();
				$upload->exts=array('jpg','png','gif');
				$upload->rootPath='./Upload/';
				$upload->savePath='Link/';
				$info=$upload->upload();
				
				if($info){
					$data['link_img']=$info['link_img']['savepath'].$info['link_img']['savename'];
				}
			}
			$res=M('link')->where("link_id=$link_id")->save($data);
			if($res){
				$this->success('编辑成功',U('Admin/Link/link'));
				exit();
			}else{
				$this->error('数据输入有误');
				exit();
			}
		}
		$this->display();
	}
	
	public function link_del(){
		$a_id=I('get.a_id');
		$oldarr=M('link')->field('link_img')->where("link_id='$a_id'")->find();
		$path='./Upload/'.$oldarr['link_img'];
		if(!empty($oldarr) && file_exists($path)){
			unlink($path);
		}
		//删除数据
		$res=M('link')->where("link_id=$a_id")->delete();
		if($res){
			echo 1;exit();
		}else{
			echo M('link')->getLastSql();exit();
		}
	}
	
		
	
}